%%%-------------------------------------------------------------------
%%% @author Evgeniy Khramtsov <ekhramtsov@process-one.net>
%%% @copyright (C) 2013, Evgeniy Khramtsov
%%% @doc
%%%
%%% @end
%%% Created :  1 May 2013 by Evgeniy Khramtsov <ekhramtsov@process-one.net>
%%%-------------------------------------------------------------------

{require_min_otp_vsn, "16"}.

Cfg = case file:consult("vars.config") of
          {ok, Terms} ->
              Terms;
          _Err ->
              []
      end,

Macros = lists:flatmap(
           fun({roster_gateway_workaround, true}) ->
                   [{d, 'ROSTER_GATEWAY_WORKAROUND'}];
              ({transient_supervisors, false}) ->
                   [{d, 'NO_TRANSIENT_SUPERVISORS'}];
              ({nif, true}) ->
                   [{d, 'NIF'}];
              ({db_type, mssql}) ->
                   [{d, 'mssql'}];
              ({lager, true}) ->
                   [{d, 'LAGER'}];
              ({erlang_deprecated_types, true}) ->
                   [{d, 'ERL_DEPRECATED_TYPES'}];
              (_) ->
                   []
           end, Cfg),

DebugInfo = case lists:keysearch(debug, 1, Cfg) of
                {value, {debug, true}} ->
                    [];
                _ ->
                    [no_debug_info]
            end,

HiPE = case lists:keysearch(hipe, 1, Cfg) of
           {value, {hipe, true}} ->
               [native];
           _ ->
               []
       end,

SrcDirs = lists:foldl(
            fun({tools, true}, Acc) ->
                    [tools|Acc];
               (_, Acc) ->
                    Acc
            end, [], Cfg),

Deps = [{p1_cache_tab, ".*", {git, "https://github.com/processone/cache_tab", "f7ea12b0ba962a3d2f9a406d2954cf7de4e27230"}},
        {p1_tls, ".*", {git, "https://github.com/processone/tls", "2e06ed6ae7d48fe469a2c9cb0869e756105a72f0"}},
        {p1_stringprep, ".*", {git, "https://github.com/processone/stringprep", "3c640237a3a7831dc39de6a6d329d3a9af25c579"}},
        {p1_xml, ".*", {git, "https://github.com/processone/xml", "7ff90b7a06ee842543bcb47564e8f0e3d3119efd"}},
        {esip, ".*", {git, "https://github.com/processone/p1_sip", "d662d3fe7f6288b444ea321d854de0bd6d40e022"}},
        {emysql, ".*", {git, "https://github.com/eonblast/emysql", "c7e2103f8b737667f0128802e8de2f0d5ed2fa5c"}},
        {p1_stun, ".*", {git, "https://github.com/processone/stun", "61d90fd9e11fbacb128ebf5cc227d75f6b7e1933"}},
        {p1_yaml, ".*", {git, "https://github.com/processone/p1_yaml", "e1f081e1bbf34c35fc60c1b988d62a07fbead3e8"}},
        {p1_utils, ".*", {git, "https://github.com/processone/p1_utils", "1bf99f9c8daed3b03f76e2714ca102c520c88b26"}}],

ConfigureCmd = fun(Pkg, Flags) ->
                       {'get-deps',
                        "sh -c 'cd deps/" ++ Pkg ++
                            " && ./configure" ++ Flags ++ "'"}
               end,

XMLFlags = lists:foldl(
             fun({nif, true}, Acc) ->
                     Acc ++ " --enable-nif";
                ({full_xml, true}, Acc) ->
                     Acc ++ " --enable-full-xml";
                (_, Acc) ->
                     Acc
             end, "", Cfg),

PostHooks = [ConfigureCmd("p1_tls", ""),
             ConfigureCmd("p1_stringprep", ""),
             ConfigureCmd("p1_yaml", ""),
	     ConfigureCmd("esip", ""),
             ConfigureCmd("p1_xml", XMLFlags)],

CfgDeps = lists:flatmap(
            fun({mysql, true}) ->
                    [{p1_mysql, ".*", {git, "https://github.com/processone/mysql", "d568bbe317c4e86ee58a603c46a98809c1279013"}}];
               ({pgsql, true}) ->
                    [{p1_pgsql, ".*", {git, "https://github.com/processone/pgsql", "e72c03c60bfcb56bbb5d259342021d9cb3581dac"}}];
               ({sqlite, true}) ->
                    [{sqlite3, ".*", {git, "https://github.com/alexeyr/erlang-sqlite3", "d8601fe5cfcc69969aca9b3d707c256fe0ae1103"}}];
               ({pam, true}) ->
                    [{p1_pam, ".*", {git, "https://github.com/processone/epam", "d3ce290b7da75d780a03e86e7a8198a80e9826a6"}}];
               ({zlib, true}) ->
                    [{p1_zlib, ".*", {git, "https://github.com/processone/zlib", "e3d4222b7aae616d7ef2e7e2fa0bbf451516c602"}}];
               ({riak, true}) ->
                    [{riakc, ".*", {git, "https://github.com/basho/riak-erlang-client", {tag, "1.4.2"}}}];
               ({json, true}) ->
                    [{jiffy, ".*", {git, "https://github.com/davisp/jiffy", "e008c0c3fffb4f509c5ae6b73b960663d778f231"}}];
               ({elixir, true}) ->
                    [{rebar_elixir_plugin, ".*", {git, "https://github.com/yrashk/rebar_elixir_plugin", "7058379b7c7e017555647f6b9cecfd87cd50f884"}},
                     {elixir, ".*", {git, "https://github.com/elixir-lang/elixir", "1d9548fd285d243721b7eba71912bde2ffd1f6c3"}}];
               ({iconv, true}) ->
                    [{p1_iconv, ".*", {git, "https://github.com/processone/eiconv", "8b7542b1aaf0a851f335e464956956985af6d9a2"}}];
               ({lager, true}) ->
                    [{lager, ".*", {git, "https://github.com/basho/lager", "26540665e640872718d3dd9c35f2addf1279114b"}}];
               ({lager, false}) ->
                    [{p1_logger, ".*", {git, "https://github.com/processone/p1_logger", "3e19507fd5606a73694917158767ecb3f5704e3f"}}];
               ({tools, true}) ->
                    [{meck, "0.*", {git, "https://github.com/eproxus/meck", "fc362e037f424250130bca32d6bf701f2f49dc75"}}];
               ({redis, true}) ->
                    [{eredis, ".*", {git, "https://github.com/wooga/eredis", "770f828918db710d0c0958c6df63e90a4d341ed7"}}];
               (_) ->
                    []
            end, Cfg),

CfgPostHooks = lists:flatmap(
                 fun({pam, true}) ->
                         [ConfigureCmd("p1_pam", "")];
                    ({zlib, true}) ->
                         [ConfigureCmd("p1_zlib", "")];
                    ({iconv, true}) ->
                         [ConfigureCmd("p1_iconv", "")];
                    (_) ->
                         []
                 end, Cfg),

CfgXrefs = lists:flatmap(
	     fun({mysql, false}) ->
		     ["(\".*mysql.*\":_/_)"];
		({pgsql, false}) ->
		     ["(\".*pgsql.*\":_/_)"];
		({pam, false}) ->
		     ["(\"epam\":_/_)"];
		({riak, false}) ->
		     ["(\"riak.*\":_/_)"];
		({riak, true}) ->
		     % used in map-reduce function called from riak vm
		     ["(\"riak_object\":_/_)"];
		({json, false}) ->
		    ["(\"jiffy\":_/_)"];
		({zlib, false}) ->
		     ["(\"ezlib\":_/_)"];
		({http, false}) ->
		     ["(\"lhttpc\":_/_)"];
		({iconv, false}) ->
		     ["(\"iconv\":_/_)"];
		({odbc, false}) ->
		     ["(\"odbc\":_/_)"];
		(_) ->
		     []
            end, Cfg),

ElixirConfig = case lists:keysearch(elixir, 1, Cfg) of
                   {value, {elixir, true}} ->
                       [{plugins, [rebar_elixir_compiler, rebar_exunit] },
                        {lib_dirs, ["deps/elixir/lib"]}];
                   _ ->
                       []
               end,

{ok, Cwd} = file:get_cwd(),

TestConfigFile = filename:join([Cwd, "test", "config.ctc"]),
TestConfig = case file:read_file_info(TestConfigFile) of
                 {ok, _} ->
                     "-userconfig ct_config_plain " ++ TestConfigFile ++ " ";
                 _ ->
                     ""
             end,

Config = [{erl_opts, Macros ++ HiPE ++ DebugInfo ++
               [{src_dirs, [asn1, src | SrcDirs]}]},
          {sub_dirs, ["rel"]},
          {keep_build_info, true},
          {ct_extra_params, "-ct_hooks cth_surefire "
           ++ TestConfig
           ++ "-include " ++ filename:join([Cwd, "tools"])},
	  {xref_warnings, false},
          {xref_checks, []},
	  {xref_queries,
	   [{"(XC - UC) || (XU - X - B - "
	     ++ string:join(CfgXrefs, " - ") ++ ")", []}]},
          {post_hooks, PostHooks ++ CfgPostHooks},
          {deps, Deps ++ CfgDeps}] ++ ElixirConfig,
%%io:format("ejabberd configuration:~n  ~p~n", [Config]),
Config.

%% Local Variables:
%% mode: erlang
%% End:
%% vim: set filetype=erlang tabstop=8:
